package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.product.pojo.param.PictureAddNewParam;
import cn.tedu.csmall.product.pojo.vo.PictureListItemVO;
import cn.tedu.csmall.product.service.PictureService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "图片管理模块")
@RequestMapping("/pictures")
@Validated
public class PictureController {

    @Autowired
    PictureService pictureService;

    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/pms/picture/add-new')")
    @ApiOperation("添加图片")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid PictureAddNewParam pictureAddNewParam) {
        log.debug("开始处理【添加图片】的请求，参数:{}", pictureAddNewParam);
        pictureService.addNew(pictureAddNewParam);
        return JsonResult.ok();
    }


    @GetMapping("/list-by-album")
    @PreAuthorize("hasAuthority('/pms/picture/read')")
    @ApiOperation("根据相册ID查询图片数据列表")
    @ApiOperationSupport(order = 421)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "albumId",value = "相册ID",required = true,dataType = "long")
    })
    public JsonResult listByAlbumId(@Range(message = "请提交有效的相册ID值！") Long albumId,
                                    @Range(min = 1, message = "请提交有效的页码值！")Integer page,
                                    String queryType) {
        log.debug("开始处理【根据相册ID查询图片列表】的请求，参数:{}", albumId);
        if (page ==null || page <0){
            page = 1;
        }
        PageData<PictureListItemVO> pageData = null;
        if ("all".equals(queryType)){
            pageData = pictureService.listByAlbumId(albumId,page,Integer.MAX_VALUE);
        } else {
            pageData = pictureService.listByAlbumId(albumId,page);
        }
        return JsonResult.ok(pageData);
    }
}
